﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Repositories.SearchExtensions;
using WebSite.ViewModels.ViewModelExtensions.DetailsViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.EditViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.IndexViewModelExtensions;

namespace WebSite.Controllers.ControllerExtensions
{
    public class PedidoControllerJolitexExtension : ControllerJolitexExtensionBase<Pedido>
    {
        protected override IIndexViewModelExtension DoGetIndexViewModelExtension(FormCollection formCollection, IRepository repository)
        {
            string codigoJolitex = formCollection["codigoJolitex"];
            IEnumerable<Fabrica> fabricaFilter = repository.User.Filtros.OfType<Fabrica>();
            bool hasFabricaPermission = repository.User.HasPermission(Area.Fabricas, Operacao.View) &&
                (!fabricaFilter.Any() || fabricaFilter.Any(x => x.FabricaJolitex != null && x.FabricaJolitex.IsJolitex));
            return new PedidoJolitexIndexViewModel(codigoJolitex, GetSincronizado(formCollection), GetAllJolitex(formCollection), hasFabricaPermission);
        }

        public override IEditViewModelExtension EditViewModelExtension
        {
            get { return new PedidoJolitexEditViewModel(); }
        }

        public override IDetailsViewModelExtension DetailsViewModelExtension
        {
            get { return new PedidoJolitexDetailsViewModel(); }
        }

        public override ISearchExtension GetSearchExtension(FormCollection formCollection)
        {
            return new PedidoJolitexSearchExtension(GetCodigoJolitex(formCollection), GetSincronizado(formCollection), GetAllJolitex(formCollection));
        }

        public override void RegisterIndexColumns(IIndexColumnsProvider indexColumnsProvider)
        {
            indexColumnsProvider.RegisterColumn("Pedido Jolitex", PedidoJolitexIndexColumns.CodigoJolitex);
            indexColumnsProvider.RegisterColumn("Sincronizado", PedidoJolitexIndexColumns.Sincronizado);
        }

        private static bool? GetSincronizado(FormCollection formCollection)
        {
            string sincronizadoString = formCollection["sincronizado"];
            bool sincronizadoBool;
            return Boolean.TryParse(sincronizadoString, out sincronizadoBool) ? (bool?) sincronizadoBool : null;
        }

        private static bool GetAllJolitex(FormCollection formCollection)
        {
            string allJolitexString = formCollection["allJolitex"];
            return allJolitexString == "true,false" || allJolitexString == true.ToString();
        }

        protected override Dictionary<string, string> TryUpdateModel(Pedido model, FormCollection formCollection)
        {
            model.PedidoJolitex.Codigo = GetCodigoJolitex(formCollection);
            return new Dictionary<string, string>();
        }

        private static string GetCodigoJolitex(FormCollection formCollection)
        {
            return formCollection["codigoJolitex"];
        }

        protected override Area Area { get { return Area.Pedidos; } }

        protected override void SetModel(Pedido model)
        {
            if (model.PedidoJolitex == null)
            {
                model.SetPedidoJolitex();
                model.PedidoJolitex.Sincronizado = false;
            }
        }
    }
}